package algotithm.leetcode.twopoint.test31;

/**
 * @author zhouyanxiang
 * @Date 2021-04-2021/4/12-22:25
 * @Title
 */
public class Solution {

    public static void main(String[] args) {
        int[] nums = {1,2,3};
        Solution solution = new Solution();
        solution.nextPermutation(nums);
        for (int i = 0 ;i < nums.length; i++) {
            System.out.print(nums[i] + " ");
        }
    }

    public void nextPermutation(int[] nums) {
        int n = nums.length;
        int i = n - 2;
        while (i >= 0 && nums[i] >= nums[i+1]) {
            i--;
        }
        if (i >= 0) {
            int j = n - 1;
            while (j >= 0 && nums[i] >= nums[j]) {
                j--;
            }
            swap(nums,i,j);
        }
        reverse(nums,i+1);
    }

    public void swap(int[] nums,int i,int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

    public void reverse(int[] nums,int start) {
        int l = start;
        int r = nums.length - 1;
        while (l < r) {
            swap(nums,l,r);
            l++;
            r--;
        }
    }
}
